package com.efuture.servlet.request;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @description: 演示获取请求参数的通用方法
 * @author zhaoshanshan
 * @date 2021/10/2 10:47 下午
 * @version 1.0
 */
@WebServlet("/GetRequestParameter")
public class GetRequestParameter extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//        1. String getParameter(String name):根据参数名称获取参数值    username=zs&password=123
          System.out.println(request.getContentType());
          String userName =  request.getParameter("userName");
          String passWord = request.getParameter("passWord");
          System.out.println(userName);
          System.out.println(passWord);
//        2. String[] getParameterValues(String name):根据参数名称获取参数值的数组  hobby=xx&hobby=game
         String[] hobbyValue = request.getParameterValues("hobby");
         for (String hobby :hobbyValue){
             System.out.println(hobby);
         }

//        3. Enumeration<String> getParameterNames():获取所有请求的参数名称
        System.out.println("---------------");
        Enumeration<String>  parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()){
            System.out.println(parameterNames.nextElement());
        }
//      4. Map<String,String[]> getParameterMap():获取所有参数的map集合
        System.out.println("---------------");
        Map<String, String[]>  parameterMap = request.getParameterMap();
        Set<String> setParameter = parameterMap.keySet();
        for(String key:setParameter){
            System.out.println(key);
            String[] name = parameterMap.get(key);
            for(String value : name){
                System.out.println(value);
            }
        }

    }
}
